__author__ = 'Administrator'
#coding=utf-8
import wx
#from wxPython.lib.editor import images
from wx.lib.embeddedimage import PyEmbeddedImage
import pic
import images

frame = None
class PopWinFrame(wx.Frame):
    def __init__(self, parent, pos=(400, 400), size=(400, 300)):
        wx.Frame.__init__(self, parent, -1, "Shaped Window", pos, size,
#                         style = wx.FRAME_NO_TASKBAR | wx.STAY_ON_TOP
                            )
        self.hasShape = False
        self.delta = (0,0)

        self.panel = wx.Panel(self)
        self.timer = wx.Timer(self)
#        self.timer.Start(100000)

        listdata = {
                    1 : ("Bad English", "The Price Of Love", "Rock"),
                    2 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
                    3 : ("George Michael", "Praying For Time", "Rock")
                    }
        self.list = wx.ListCtrl(self.panel, size=size,
#                                style=wx.LC_REPORT
#                                | wx.BORDER_NONE
#                                | wx.LC_EDIT_LABELS
#                                | wx.LC_SORT_ASCENDING
        )
        for key in listdata.keys():
            self.list.Append(listdata[key])
        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(self.list, 1, wx.EXPAND|wx.ALL, 5)
        self.panel.SetSizer(box)

        self.il = wx.ImageList(32, 32)
        bmp = PyEmbeddedImage(pic.pic1).GetBitmap()
        self.idx1 = self.il.Add(bmp)
        self.list.SetImageList(self.il, wx.IMAGE_LIST_SMALL)

        self.list.InsertImageStringItem(0, 'hello', self.idx1)
        self.list.InsertImageItem(0, self.idx1)
        
        ## 创建右键弹出式菜单
        self.popupmenu = wx.Menu()#创建一个菜单
        for text in "one two three four five Close".split():#填充菜单
            item = self.popupmenu.Append(-1, text)
            if text == 'Close':
                self.Bind(wx.EVT_MENU, self.OnExit, item)
        self.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup)#绑定一个显示菜单事件

        self.Bind(wx.EVT_LEFT_DOWN,     self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP,       self.OnLeftUp)
        self.Bind(wx.EVT_MOTION,        self.OnMouseMove)
#        self.Bind(wx.EVT_TIMER,         self.OnTimer)


        # 显示一个按钮
#        bmp = PyEmbeddedImage(pic.pic1).GetBitmap()
#        mask = wx.Mask(bmp, wx.BLUE)
#        bmp.SetMask(mask)
#        b = wx.BitmapButton(self, -1, bmp, (20, 20),
#                       (bmp.GetWidth()+10, bmp.GetHeight()+10))
#        b.SetToolTipString("This is a bitmap button.")

    def OnShowPopup(self, event):#弹出显示
         pos = event.GetPosition()
         pos = self.ScreenToClient(pos)
         self.PopupMenu(self.popupmenu, pos)

    def OnExit(self, evt):
#        self.Close()
        self.Hide()

    def OnLeftDown(self, evt):
        self.CaptureMouse()
        x, y = self.ClientToScreen(evt.GetPosition())
        originx, originy = self.GetPosition()
        dx = x - originx
        dy = y - originy
        self.delta = ((dx, dy))

    def OnLeftUp(self, evt):
        if self.HasCapture():
            self.ReleaseMouse()

    def OnMouseMove(self, evt):
        if evt.Dragging() and evt.LeftIsDown():
            x, y = self.ClientToScreen(evt.GetPosition())
            fp = (x - self.delta[0], y - self.delta[1])
            self.Move(fp)
            
#    def OnTimer(self, evt):
#        self.Close()

#############################################################################

def isShow():
    if frame:
        return frame.IsShown()

def init():
    print 'pyPopWin : Init'
    global frame
    frame = PopWinFrame(None)
    frame.Show(True)

def Show(isShow):
    print 'pyPopWin : Show'
    global frame
    if frame == None:
        init()
    else:
        frame.Show(isShow)

def Exit():
    print 'pyPopWin : Exit'
    global frame
    if frame:
        frame.Close()
        frame = None

###############################################################
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = PopWinFrame(None)
    frame.Show(True)
    app.MainLoop()
    print 'pyPopWin: Destroy'
    